namespace CSharpPro._1106;


// User类 - 只包含数据
public class User
{
    // 字段
    private string _username;
    private string _password;
    private string _email;
    private DateTime _registrationDate;

    // 属性
    public string Username
    {
        get => _username;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("用户名不能为空");
            if (value.Length < 3 || value.Length > 15)
                throw new ArgumentException("用户名长度必须为3-15个字符");
            if (!System.Text.RegularExpressions.Regex.IsMatch(value, @"^[a-zA-Z0-9_]+$"))
                throw new ArgumentException("用户名只能包含字母、数字、下划线");
            _username = value;
        }
    }

    public string Password
    {
        get => _password;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("密码不能为空");
            if (value.Length < 6 || value.Length > 20)
                throw new ArgumentException("密码长度必须为6-20个字符");
            if (!System.Text.RegularExpressions.Regex.IsMatch(value, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$"))
                throw new ArgumentException("密码必须包含至少1个大写字母、1个小写字母和1个数字");
            _password = value;
        }
    }

    public string Email
    {
        get => _email;
        set
        {
            if (string.IsNullOrWhiteSpace(value))
                throw new ArgumentException("邮箱不能为空");
            if (!System.Text.RegularExpressions.Regex.IsMatch(value, @"^[^@\s]+@[^@\s]+\.[^@\s]+$"))
                throw new ArgumentException("邮箱格式不正确");
            _email = value;
        }
    }

    public DateTime RegistrationDate
    {
        get => _registrationDate;
        set => _registrationDate = value;
    }

    // 构造函数
    public User(string username, string password, string email)
    {
        Username = username;
        Password = password;
        Email = email;
        RegistrationDate = DateTime.Now;
    }

    // 默认构造函数
    public User() { }

    // 重写ToString方法
    public override string ToString()
    {
        return $"用户名: {Username}, 邮箱: {Email}, 注册时间: {RegistrationDate:yyyy-MM-dd HH:mm:ss}";
    }

    // 重写Equals和GetHashCode
    public override bool Equals(object obj)
    {
        return obj is User user && Username == user.Username;
    }

    public override int GetHashCode()
    {
        return Username.GetHashCode();
    }
}
